package com.chatcompetition.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chatcompetition.domain.entity.Employee;
import com.chatcompetition.mapper.EmployeeMapper;
import com.chatcompetition.service.EmployeeService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.List;

@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public Employee login(String employeeNumber, String employeePassword, String employeeDepartment) {
//
//        //密码比对
//        //对前端传过来的明文密码进行md5加密处理
//        password = DigestUtils.md5DigestAsHex(password.getBytes());
//        if (!password.equals(employee.getPassword())) {
//            //密码错误
//            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
//        }
////
//        if (employee.getStatus() == StatusConstant.DISABLE) {
//            //账号被锁定
//            throw new AccountLockedException(MessageConstant.ACCOUNT_LOCKED);
//        }

        return employeeMapper.findByEmployeeNumberAndEmployeePasswordAndDepartment(employeeNumber, employeePassword, employeeDepartment);
    }


    @Override
    public List<Employee> getAllEmployees() {
        return employeeMapper.selectList(null);
    }

    @Override
    public void deleteEmployee(Integer id) {
        employeeMapper.deleteById(id);
    }

    @Override
    public Employee addEmployee(Employee employee) {
        employeeMapper.insert(employee);
        return employee;
    }

    @Override
    public boolean updateEmployeeByEmployeeNumber(Employee employee) {
        // 使用 UpdateWrapper 构建更新条件
        UpdateWrapper<Employee> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("employee_number", employee.getEmployeeNumber());

        // 更新员工信息
        return employeeMapper.update(employee, updateWrapper) > 0;
    }
    @Override
    public boolean deleteByEmployeeNumber(String employeeNumber) {
        // 使用 QueryWrapper 构建查询条件
        QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("employee_number", employeeNumber);

        // 根据工号删除员工
        return employeeMapper.delete(queryWrapper) > 0;
    }

    @Override
    public boolean updateEmployeeStatusByEmployeeNumber(String employeeNumber, int employeeStatus) {
        // 使用 UpdateWrapper 构建更新条件
        UpdateWrapper<Employee> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("employee_number", employeeNumber);

        // 创建一个 Employee 对象，只更新 employeeStatus 字段
        Employee employee = new Employee();
        employee.setEmployeeStatus(employeeStatus);

        // 更新员工状态
        return employeeMapper.update(employee, updateWrapper) > 0;
    }


    @Override
    public Integer getEmployeeStatusByEmployeeNumber(String employeeNumber) {
        // 调用 Mapper 层方法，查询员工的 employee_status
        return employeeMapper.findEmployeeStatusByEmployeeNumber(employeeNumber);
    }
}